//
//  WmcCloudMineCell.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/31.
//

import UIKit

class WmcCloudMineCell: WmcBaseTableCell {
    lazy var coverLabel: UILabel = {
        let label = UILabel.YP.Create(font: 15.yp.uiFont(weight: .medium),
                                      color: UIColor.white,
                                      alignment: .center)
        label.backgroundColor = .wmc_mainTheme
        label.layer.cornerRadius = 3
        label.layer.masksToBounds = true
        label.isHidden = true
        return label
    }()

    lazy var avatarView: WmcMemberAvatarView = {
        let view = WmcMemberAvatarView()
        return view
    }()
    
    lazy var disTitleLabel: UILabel = {
        let label = UILabel.YP.Create(font: 17,
                                      color: UIColor.white,
                                      alignment: .right)
        label.isHidden = true
        return label
    }()

    lazy var switchBtn: UISwitch = {
        let switc = UISwitch()
        switc.onTintColor = .wmc_mainTheme
        switc.isHidden = true
        switc.addTarget(self, action: #selector(onSwitchAction(sender:)), for: .valueChanged)
        return switc
    }()

    private(set) var model: WmcCloudMineVM.CellModel?

    var switchDidChange: ((Bool, WmcCloudMineVM.CellModel) -> Void)?

    @objc
    func onSwitchAction(sender: UISwitch) {
        if sender.isOn {
            guard let model = self.model else { return }
            self.switchDidChange?(sender.isOn, model)
            WmcUploadQueueManager.startAutoUploadTask()
        } else {
            let alert = WmcAlertView()
            alert.title = "提示"
            alert.msg = "关闭后，拍摄的照片将无法自动同步到项目中"
            alert.style = .custom(confirmTitle: "确认关闭", cancelTitle: "再想想")
            alert.confirmBlock = { [weak self] in
                guard let weakSelf = self,
                      let model = weakSelf.model else { return }
                weakSelf.switchDidChange?(sender.isOn, model)
                WmcUploadQueueManager.stopAllTask()
            }
            alert.cancelBlock = { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.switchBtn.isOn = true
            }
            alert.show()
        }
    }

    override func makeUI() {
        titleLabel.textColor = .wmc_grayH2_323233
        titleLabel.font = 17.wmc_font
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.bottom.right.equalToSuperview()
            make.left.equalTo(Wmc_standardEdge.left)
        }

        addSubview(coverLabel)
        coverLabel.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.bottom.equalTo(-18)
            make.right.equalTo(-Wmc_standardEdge.right-Wmc_standardEdge.right)
            make.width.equalTo(coverLabel.snp.height)
        }

        addSubview(avatarView)
       
        
        addSubview(arrowImageView)
        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-Wmc_standardEdge.right).offset(-8)
            make.size.equalTo(24)
        }
        
        avatarView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(arrowImageView.snp.left).offset(-8)
            //make.width.equalTo(coverLabel.snp.height)
            make.width.height.equalTo(36)
        }

        addSubview(disTitleLabel)
        disTitleLabel.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.right.equalTo(arrowImageView.snp.left).offset(-8)
        }

        addSubview(switchBtn)
        switchBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.equalTo(Wmc_standardEdge.left)
            make.bottom.equalToSuperview()
            make.right.equalTo(-Wmc_standardEdge.right)
            make.height.equalTo(0.5)
        }
        
        arrowImageView.contentMode = .scaleAspectFill
        arrowImageView.image = "teamAlbumRightArrow".wmc_image
    }

    func configModel(_ model: WmcCloudMineVM.CellModel) {
        self.model = model

        titleLabel.text = model.title
        // 样式
        coverLabel.isHidden = true
        avatarView.isHidden = true
        arrowImageView.isHidden = true
        disTitleLabel.isHidden = true
        switchBtn.isHidden = true
        
        self.updateUI(model)
        
        switch model.style {
        case .none:
            arrowImageView.isHidden = false
            disTitleLabel.textColor = .wmc_grayH2_323233
            titleLabel.textColor = .wmc_grayH2_323233
        case let .userAvatar(name, img):
            coverLabel.isHidden = false
            avatarView.isHidden = false
            arrowImageView.isHidden = false
            coverLabel.isHidden = true
            coverLabel.text = name.yp.keepSuffix(count: 2)
            let member = WmcTeamMember.init(name: name, img: img)
            avatarView.member = member
        case let .cover(name):
            coverLabel.isHidden = false
            avatarView.isHidden = false
            arrowImageView.isHidden = false
            coverLabel.text = name.yp.keepSuffix(count: 2)
        case let .text(text, color: color, font: font):
            disTitleLabel.isHidden = false
            disTitleLabel.text = text
            disTitleLabel.font = font
            disTitleLabel.textColor = color
            titleLabel.textColor = .wmc_grayH2_323233
            arrowImageView.isHidden = false
        case let .switch(isOn):
            switchBtn.isHidden = false
            switchBtn.isOn = isOn
        case let .onyAction(textColor):
            self.disTitleLabel.isHidden = true
            self.arrowImageView.isHidden = true
            self.titleLabel.textColor = textColor
        }
    }
    
    func updateUI(_ model: WmcCloudMineVM.CellModel) -> Void {
        
        switch model.style {
        case .none,.cover,.switch,.text,.userAvatar:
            self.titleLabel.textAlignment = .left
        case .onyAction:
            self.titleLabel.textAlignment = .center
        }
    }
}
